<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript">
			function get(){
				//收集编号
				var $uid=uid.value;
				if(!$uid){alert("编号不能为空");return}
				//1.
				var xhr=new XMLHttpRequest(); 
				//4.
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4 && xhr.status==200){
						var r=xhr.responseText;
						console.log(r)
						if(r==0){
							alert("查询失败")
						}else{
							var arr=JSON.parse(r);
							uname.value=arr[0].uname;
							upwd.value=arr[0].upwd;
							email.value=arr[0].email;
							phone.value=arr[0].phone;
							user_name.value=arr[0].user_name;
						}
					}
				}
				//2.
				xhr.open("GET",`/user/v1/search/${$uid}`,true);
				//3.
				xhr.send();
			}
			function update(){
				//收集用户信息
				var $uid=uid.value;
				var $uname=uname.value;
				var $upwd=upwd.value;
				var $email=email.value;
				var $phone=phone.value;
				var $user_name=user_name.value;
				var $gender="保密";
				if(man.checked){
					$gender=1
				}else if(woman.checked){
					$gender=0
				}else{
					$gender=-1
				}
				if(!$uid){alert("编号不能为空");return}
				if(!$uname){alert("网名不能为空");return}
				if(!$upwd){alert("密码不能为空");return}
				if(!$email){alert("邮件不能为空");return}
				if(!$phone){alert("手机号码不能为空");return}
				if(!$user_name){alert("用户名不能为空");return}
				//1.创建xhr异步对象
				var xhr=new XMLHttpRequest();
				//4.接收响应
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4 && xhr.status==200){
						var r=xhr.responseText;
						if(r==1){
							alert("修改成功")
						}else{
							alert("修改失败")
						}
					}
				}
				//2.创建请求,打开连接
				xhr.open("PUT",`/user/v1/update`,true)
				//3.发送请求
				var formdata=`uid=${$uid}&uname=${$uname}&upwd=${$upwd}&phone=${$phone}&email=${$email}&user_name=${$user_name}&gender=${$gender}`;
				console.log(formdata)
				xhr.setRequestHeader("content-type","application/x-www-form-urlencoded")
				xhr.send(formdata);
			}
		</script>
	</head>
	<body>
		<input type="text" id="uid">编号<br>
		<button onclick="get()">查询</button><br>
		<input type="text" id="uname">网名<br>
		<input type="text" id="upwd">密码<br>
		<input type="text" id="email">邮件<br>
		<input type="text" id="phone">手机号码<br>
		<input type="text" id="user_name">用户名<br>
		<input type="radio" name="gender" checked id="man" />男
		<input type="radio" name="gender" id="woman" />女
		<input type="radio" name="gender" id="unk" />保密<br>
		<button onclick="update()">修改</button>
	</body>
</html>
